﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tasks.UI.Models;
using Tasks.Common.Entitys;
using System.Collections;
using Tasks.Business;
using Tasks.Core;
using Tasks.Common;
using Tasks.UI.Models.Dict;

namespace Tasks.UI.Controllers
{
    public class DictController : BaseController
    {
        /// <summary>
        /// 字典管理，显示指定字典列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequireLogin]
        public ActionResult Manage(DictManageModel model)
        {
            #region "权限控制"

            if (!this.ValidateDictPower(model.DictKey, "view"))
                return Content("不具备操作权限");

            #endregion

            //执行删除动作
            if (model.act == "del" && model.DictID != 0)
            {
                ExecuteResult result = BusinessFactory.BasedataBLL.DeleteDict(model.DictID);
            }

            //查询
            int rowsCount = 0;
            model.PageSize = model.PageSize == 0 ? 10 : model.PageSize;
            BasedataBLL bll = BusinessFactory.BasedataBLL;

            DictGroup group = bll.GetDictGroupByKey(model.DictKey);
            model.Title = group.Name;

            if (group.DataType == "KEY_VALUE"
                || group.DataType == "CONFIG")
                return RedirectToAction("KeyValueManage", new { DictKey = model.DictKey });


            Hashtable condition = new Hashtable();
            condition["DictKey"] = model.DictKey;
            condition["Name"] = model.Name;
            if (model.PageIndex < 1)
            {
                model.PageIndex = 1;
            }
            IList<DictValue> dicts = bll.SearchDictByCondition(condition, model.PageIndex, model.PageSize, out rowsCount);

            model.Dicts = dicts;
            model.RowCount = rowsCount;
            if (Config.SystemTheme == "default" && model.typeName == "dinn")
            {
                return View("~/Views/SystemSetting/DiningStandardSet.cshtml", model);
            }
            else if (Config.SystemTheme == "default" && model.typeName == "attention")
            {
                return View("~/Views/SystemSetting/attentionSet.cshtml", model);
            }
            else if (Config.SystemTheme == "default" && model.typeName == "shoptype")
            {
                return View("~/Views/SystemSetting/shopTypeSet.cshtml", model);
            }
            else if (Config.SystemTheme == "default" && model.typeName == "guiderequire")
            {
                return View("~/Views/SystemSetting/GuideRequireSet.cshtml", model);
            }

            return View(model);
        }

        /// 保存字典信息
        [RequireLogin]
        public ActionResult DoSaveDictInfo(DictManageModel model)
        {
            ExecuteResult result;

            #region "权限控制"

            if (!this.ValidateDictPower(model.DictKey, "manage"))
                return Json(new ExecuteResult() { ResultNo = 2, Message = "不具备操作权限" });

            #endregion

            DictValue dict = new DictValue()
            {
                SNO = model.sno,
                DictKey = model.DictKey,
                Name = model.Name,
                Content = model.Content,
                State = 1,
                ID = model.DictID
            };

            if (model.DictID == 0)
            {
                result = BusinessFactory.BasedataBLL.CreateDict(dict);
            }
            else
            {
                result = BusinessFactory.BasedataBLL.ModifyDict(dict);
            }
            return Json(result);
        }

        /// 删除字典值
        [RequireLogin]
        public ActionResult DoDeleteDict(DictManageModel model)
        {
            ExecuteResult result;

            #region "权限控制"

            if (!this.ValidateDictPower(model.DictKey, "manage"))
                return Json(new ExecuteResult() { ResultNo = 2, Message = "不具备操作权限" });

            #endregion

            result = BusinessFactory.BasedataBLL.DeleteDict(model.DictID);
            return Json(result);
        }

        /// <summary>
        /// 键值类型的数据字典管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult KeyValueManage(KeyValueManageModel model)
        {
            #region "权限控制"

            if (!this.ValidateDictPower(model.DictKey, "view"))
                return Content("不具备操作权限");

            #endregion

            DictBLL dictBLL = new DictBLL();
            model.DictGroup = dictBLL.GetDictGroupByKey(model.DictKey);
            if (model.DictGroup.DataType == "CONFIG")
                model.Dicts = dictBLL.GetDictsKeyValueByOwnerID(model.DictKey, Config.CompanyID);
            else if (model.DictGroup.DataType == "KEY_VALUE")
                model.Dicts = dictBLL.GetDictsByDictKey(model.DictKey, Config.CompanyID);

            return View(model);
        }

        /// <summary>
        /// 验证是否具备操作数据字典的权限
        /// </summary>
        /// <param name="DictKey"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        public bool ValidateDictPower(string DictKey, string act)
        {
            Power power = new Power();

            if (
                (DictKey == "SITE_GLOBAL_SET"
                || DictKey == "SITE_HOME_SET"
                || DictKey == "SITE_AROUND_SET"
                || DictKey == "SITE_DOMESTIC_SET"
                || DictKey == "SITE_HOTEL_SET"
                || DictKey == "SITE_SCENERY_SET"
                )
                && power.ValidatePower("website", "setting")
                )
            {
                return true;
            }
            //else if (act == "view" && power.ValidatePower("dict", "view"))
            //{
            //    return true;
            //}
            //else if (act == "manage" && power.ValidatePower("dict", "manage"))
            //{
            //    return true;
            //}

            return true;
        }

        [HttpPost, ActionName("KeyValueManage")]
        public ActionResult doKeyValueManage(KeyValueManageModel model)
        {

            #region "权限控制"

            if (!this.ValidateDictPower(model.DictKey, "manage"))
                return Json(new ExecuteResult() { ResultNo = 2, Message = "不具备操作权限" });

            #endregion

            IList<DictValue> dicts = new List<DictValue>();

            for (int i = 0; i < model.Name.Length; i++)
            {
                dicts.Add(new DictValue()
                {
                    DictKey = model.DictKey,
                    Name = model.Name[i],
                    Content = model.Content[i],
                    OwnerID = Config.CompanyID,
                    State = 1
                });
            }

            new DictBLL().UpdateKeyValue(model.DictKey, Config.CompanyID, dicts);
            model.MessageType = MessageType.correct;
            model.Message = "保存成功";

            return RedirectToAction("KeyValueManage", new { DictKey = model.DictKey, Message = model.Message, MessageType = (int)model.MessageType });
        }

        /// <summary>
        /// 线路模板，字典列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequireLogin]
        public ActionResult ManageTemplate(DictManageModel model)
        {
            //执行删除动作
            if (model.act == "del" && model.DictID != 0)
            {
                ExecuteResult result = BusinessFactory.BasedataBLL.DeleteDict(model.DictID);
            }

            //查询
            int rowsCount = 0;
            model.PageSize = model.PageSize == 0 ? 10 : model.PageSize;
            BasedataBLL bll = BusinessFactory.BasedataBLL;

            Hashtable condition = new Hashtable();
            condition["DictKey"] = model.DictKey;
            condition["Name"] = model.Name;
            condition["Content"] = model.Content;
            if (model.PageIndex < 1)
            {
                model.PageIndex = 1;
            }
            IList<DictValue> dicts = bll.SearchDictByCondition(condition, model.PageIndex, model.PageSize, out rowsCount);

            model.Dicts = dicts;
            model.RowCount = rowsCount;

            return View(model);
        }



    }
}